package com.punai.dataanalysis.srlib.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.poi.ExcelUtil;
import com.punai.dataanalysis.cblib.bo.MMAInfo;
import com.punai.dataanalysis.srlib.bo.MaterialDetailBo;
import com.punai.dataanalysis.srlib.service.MaterialMachinePriceService;
import com.punai.dataanalysis.srlib.vo.GovernmentPriceVo;
import com.punai.dataanalysis.srlib.vo.MaterialMachinePriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/dataanalysis/srlib/materialPL/materialmachine")
public class MaterialMachinePriceController extends BaseController {

    @Autowired
    private MaterialMachinePriceService materialMachinePriceService;


    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:getMainTableList')")
    @GetMapping("/getMainTableList")
    public TableDataInfo getMainTableList(MaterialMachinePriceVo vo) {
        return getDataTable(materialMachinePriceService.getMainTableList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:saveRecord')")
    @GetMapping("/saveRecord")
    public AjaxResult saveRecord(MaterialMachinePriceVo vo) {
        return AjaxResult.success(materialMachinePriceService.saveRecord(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:getRecordList')")
    @GetMapping("/getRecordList")
    public TableDataInfo getRecordList(MaterialMachinePriceVo vo) {
        return getDataTable(materialMachinePriceService.getRecordList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:deleteRecord')")
    @GetMapping("/deleteRecord")
    public AjaxResult deleteRecord(MaterialMachinePriceVo vo) {
        return AjaxResult.success(materialMachinePriceService.deleteRecord(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:getInfoList')")
    @GetMapping("/getInfoList")
    public TableDataInfo getInfoList(MaterialMachinePriceVo vo) {
        return getDataTable(materialMachinePriceService.getInfoList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:uploadInfo')")
    @PostMapping("/uploadInfo")
    public AjaxResult uploadInfo(String recordId,MultipartFile uploadFile) {
        String result = materialMachinePriceService.uploadInfo(recordId, uploadFile);
        if(StrUtil.isNotEmpty(result)) {
            return AjaxResult.error(result);
        }
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:downloadTemplete')")
    @PostMapping(value = "/downloadTemplete")
    public void downloadTemplete(String selIds, String addressId, HttpServletResponse response) {
        List<String> ids = new ArrayList<>();
        if(StrUtil.isNotEmpty(selIds)) ids = Arrays.asList(selIds.split(","));
        List<MaterialMachinePriceVo> list = materialMachinePriceService.downloadTemplete(ids, addressId);
        ExcelUtil<MaterialMachinePriceVo> util = new ExcelUtil<>(MaterialMachinePriceVo.class);
        util.exportExcel(response, list, "Sheet");
    }

    @PreAuthorize("@ss.hasPermi('srlib:materialmachinePL:downloadMaterialmachinePL')")
    @PostMapping(value = "/downloadMaterialmachinePL")
    public void downloadMaterialmachinePL(MaterialMachinePriceVo vo, HttpServletResponse response) {
        List<MaterialMachinePriceVo.ExportMianTable> list = materialMachinePriceService.downloadMaterialmachinePL(vo);
        ExcelUtil<MaterialMachinePriceVo.ExportMianTable> util = new ExcelUtil<>(MaterialMachinePriceVo.ExportMianTable.class);
        util.exportExcel(response, list, "Sheet");
    }


    @PreAuthorize("@ss.hasPermi('dataanalysis:srlib:materialmachineDetail')")
    @GetMapping("/getTableListByMaterialId")
    public AjaxResult getTableListByMaterialId(MaterialMachinePriceVo vo) {
        startPage();
        Map<String,Object> resMap = new HashMap<>();
        TableDataInfo dataTable = getDataTable(materialMachinePriceService.getTableListByMaterialId(vo));
        // 获取mma
        MaterialDetailBo detailBo = materialMachinePriceService.getMMAInfo(vo);
        resMap.put("dataTable",dataTable);
        resMap.put("detailBo",detailBo);
        return AjaxResult.success(resMap);
    }

    @PreAuthorize("@ss.hasPermi('dataanalysis:srlib:materialmachineDetail')")
    @GetMapping("/getTjtMapByMaterialId")
    public AjaxResult getTjtMapByMaterialId(MaterialMachinePriceVo vo) {
        return AjaxResult.success(materialMachinePriceService.getTjtMapByMaterialId(vo));
    }




    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable  String id) {
        return AjaxResult.success(materialMachinePriceService.selectById(id));
    }

}
